package tcc.wa.view.activity;

import java.util.List;

import tcc.wa.R;
import tcc.wa.model.Alert;
import tcc.wa.presenter.AlertPresenter;
import tcc.wa.presenter.view.AlertView;
import tcc.wa.view.activity.base.ListActivityBase;
import tcc.wa.view.adapter.AlertAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.TextView;

/**
 * Activity class for Alert management.
 */
public class AlertActivity extends ListActivityBase implements AlertView {

	private final AlertPresenter presenter;
	
	private TextView emptyAlertListTextView;
	
	public AlertActivity() {
		presenter = new AlertPresenter(this);
	}
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		presenter.onCreate();
	}
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onResume()
	 */
	@Override
	protected void onResume() {
		super.onResume();
		presenter.updateAlerts();
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onCreateContextMenu(android.view.ContextMenu, android.view.View, android.view.ContextMenu.ContextMenuInfo)
	 */
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		String[] menuItems = getResources().getStringArray(R.array.alert_menu);
		for (String menuItem : menuItems)
			menu.add(menuItem);
	}
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onContextItemSelected(android.view.MenuItem)
	 */
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
		Alert selectedAlert = (Alert) getListView().getItemAtPosition(menuInfo.position);
		presenter.deleteAlert(selectedAlert.getId());
		return true;
	}

	/* (non-Javadoc)
	 * @see tcc.wa.presenter.view.View#initView(int)
	 */
	public void initView(int layoutId) {
		setContentView(layoutId);
		emptyAlertListTextView = (TextView) findViewById(R.id.emptyAlertListTextView);
		registerForContextMenu(getListView());
	}
	
	/* (non-Javadoc)
	 * @see tcc.wa.presenter.view.AlertView#setAlertList(java.util.List)
	 */
	public void setAlertList(List<Alert> alerts) {
		if (alerts.size() > 0) {
			emptyAlertListTextView.setVisibility(View.GONE);
			getListView().setVisibility(View.VISIBLE);
			AlertAdapter adapter = new AlertAdapter(this, alerts);
			setListAdapter(adapter);
		} else {
			emptyAlertListTextView.setVisibility(View.VISIBLE);
			getListView().setVisibility(View.GONE);
		}
	}
	
	/* (non-Javadoc)
	 * @see tcc.wa.presenter.view.AlertView#displayAlertEnrollment()
	 */
	public void displayAlertEnrollment() {
		Intent intent = new Intent(this, AlertEnrollmentActivity.class);
		startActivity(intent);
	}
	
	/**
	 * Displays the Alert enrollment screen.
	 *
	 * @param view the view of origin
	 */
	public void newAlert(View view) {
		presenter.newAlert();
	}
}
